﻿#region Using Statements

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;

#endregion

namespace PrehistoricGame
{
    public class Player
    {
        Vector2 position;
        Vector2 Velocity;

        public enum PlayerState { Idle, Running, AirBorne, Attacking };
        public enum PlayerOrientation { Left, Right };

        PlayerState currentState = PlayerState.Idle;
        PlayerOrientation currentOrientation = PlayerOrientation.Right;

        AnimationPlayer animplayer = new AnimationPlayer();
        Animation a_run;
        Animation a_idle;
        Animation a_jump;
        Animation a_hits;

        public Player(Vector2 position, Animation Idle, Animation Run, Animation Jump, Animation Hits)
        {
            this.position = position;
            this.a_idle = Idle;
            this.a_run = Run;
            this.a_jump = Jump;
            this.a_hits = Hits;
            animplayer.PlayAnimation(a_idle);
        }

        public Vector2 Position 
        {
            get { return position; }
            set { position = value;}
        }

        public PlayerState CurrentState
        {
            get { return currentState; }
            set { currentState = value;}
        }

        public PlayerOrientation CurrentOrientation 
        {
            get { return currentOrientation; }
            set { currentOrientation = value; }
        }

        public void Update(GameTime gameTime, KeyboardState keyboardstate)
        {
            PlayerState newState = currentState;

            //Do state stuff
            switch (currentState)
            {
                case PlayerState.Idle:
                    {
                        if (keyboardstate.IsKeyDown(Keys.Right))
                        {
                            newState = PlayerState.Running;
                            currentOrientation = PlayerOrientation.Right;
                            Velocity.X = 500;
                        }
                        else if (keyboardstate.IsKeyDown(Keys.Left))
                        {
                            newState = PlayerState.Running;
                            currentOrientation = PlayerOrientation.Left;
                            Velocity.X = -500;
                        }
                        break;
                    }
                case PlayerState.AirBorne:
                        break;
                case PlayerState.Running:
                        {
                            if (!keyboardstate.IsKeyDown(Keys.Right) && !keyboardstate.IsKeyDown(Keys.Left))
                            {
                                Velocity.X = 0;
                                newState = PlayerState.Idle;
                            }
                            break;
                        }
                default:
                        break;
            }

            //Do animation stuff
            if (newState != currentState)
                switch (newState)
                {
                    case PlayerState.Running:
                            animplayer.PlayAnimation(a_run);
                            break;
                    case PlayerState.Idle:
                            animplayer.PlayAnimation(a_idle);
                            break;
                    default:
                        animplayer.PlayAnimation(a_idle);
                        break;
                }

            //Update position
            position += Velocity * gameTime.ElapsedGameTime.Milliseconds / 1000f;

            currentState = newState;
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 drawPosition)
        {
            animplayer.Draw(gameTime, spriteBatch, drawPosition, Vector2.One / 2f, (currentOrientation == PlayerOrientation.Right) ? SpriteEffects.FlipHorizontally : SpriteEffects.None);
        }
    }
}
